package com.midian.service.activity.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoBySql;
import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.dao.SqlHelper;
import com.midian.common.dao.SearchHelper.Type;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.DateUtils;
import com.midian.model.activity.ActivityUser;
import com.midian.model.activity.vo.ActivityUserVO;
import com.midian.service.activity.IActivityUserService;
import com.midian.service.common.BaseService;

/**
 * 活动用户（报名）业务实现类
 * 
 * @author Mengfh
 *
 */
@Service("activityUserService")
public class ActivityUserServiceImpl extends BaseService implements IActivityUserService {
	/** 活动用户（报名）DAO */
	private BaseDaoBySql<ActivityUser> activityUserDAO;

	@Resource(name = "activityUserDAO")
	public void setActivityUserDAO(BaseDaoBySql<ActivityUser> activityUserDAO) {
		this.activityUserDAO = activityUserDAO;
	}

	@Override
	public ActivityUser add(ActivityUser target) throws BizException {
		this.activityUserDAO.add(target);
		return target;
	}

	@Override
	public void update(ActivityUser target) throws BizException {
		this.activityUserDAO.updateByConvertHql(target);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.activityUserDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.activityUserDAO.delete(ids);
	}

	@Override
	public ActivityUser get(Serializable id) throws BizException {
		return this.activityUserDAO.get(id);
	}

	public List<ActivityUserVO> showList(ActivityUserVO target, Pager pager, List<OrderHelper> helper)
			throws BizException {
		StringBuilder sql = new StringBuilder(
				"SELECT o.orderSn,u.account,u.userFrom,o.joinState,a.title,a.detail,o.contactPerson as contanctName,o.contactNum as contanctPhone,o.adultCount,o.childCount,o.adultPrice,o.childPrice,o.createTime  ");
		sql.append(
				"from activity_user o LEFT OUTER JOIN user u on u.id=o.joinUserId LEFT OUTER JOIN activity a on o.activityId = a.id ");
		sql.append(" where o.deleteFlag = ?");

		ArrayList<Object> values = new ArrayList<>();
		values.add(SysStatic.NORMALDELFLAG);

		if (target != null) {
			// 过滤起始日期
			if (StringUtils.isNotBlank(target.getBeginTimeStr())) {
				Date beginDate;
				try {
					beginDate = DateUtils.formatDateStringToDate(target.getBeginTimeStr(), "yyyy-MM-dd");
					sql.append(" and o.createTime > ? ");
					values.add(beginDate);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			// 过滤结束日期
			if (StringUtils.isNotBlank(target.getEndTimeStr())) {
				Date endDate;
				try {
					endDate = DateUtils.formatDateStringToDate(target.getEndTimeStr(), "yyyy-MM-dd");
					sql.append(" and o.createTime < ? ");
					values.add(endDate);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			// 过滤会员账号
			if (StringUtils.isNotBlank(target.getAccount())) {
				sql.append(" and u.account like ? ");
				values.add("%" + target.getAccount() + "%");
			}

			// 过滤活动标题
			if (StringUtils.isNotBlank(target.getTitle())) {
				sql.append(" and a.title like ? ");
				values.add("%" + target.getTitle() + "%");
			}

			// 过滤联系人
			if (StringUtils.isNotBlank(target.getContanctName())) {
				sql.append(" and o.contactPerson like ? ");
				values.add("%" + target.getContanctName() + "%");
			}

			// 过滤联系电话
			if (StringUtils.isNotBlank(target.getContanctPhone())) {
				sql.append(" and o.contactNum like ? ");
				values.add("%" + target.getContanctPhone() + "%");
			}

			// 过滤报名状态
			if (target.getJoinState() != null) {
				sql.append(" and o.joinState = ? ");
				values.add(target.getJoinState());
			}

		}

		SqlHelper.opOrder(sql, helper);

		String[] fields = new String[] { "orderSn", "account", "userFrom", "joinState", "title", "detail",
				"contanctName", "contanctPhone", "adultCount", "adultPrice", "childCount", "childPrice", "createTime" };
		return this.activityUserDAO.pageFindBySqlWithFieldNames(sql.toString(), values.toArray(), pager,
				ActivityUserVO.class, fields, true);
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.activityUserDAO.opDelFlag(ids, status);
	}

	@Override
	protected List<SearchHelper> getSearchHelper() {
		String[] attrs = { "user", "activity", "activity.title", "user.account", "joinState", "contactPerson",
				"contactNum", "createTime", "delFlag" };
		Type[] types = { Type.LEFTFETCH, Type.LEFTFETCH, Type.LIKE, Type.LIKE, Type.EQ, Type.LIKE, Type.EQ,
				Type.BETWEEN, Type.EQ };
		return SearchHelper.getSearchList(attrs, types);
	}

	@Override
	public List<ActivityUserVO> getActivityUsers() throws BizException {
		String hql = "from ActivityUser t where t.delFlag = ?";
		return this.activityUserDAO.find(hql, new Object[] { SysStatic.NORMALDELFLAG });
	}

	@Override
	public List<ActivityUser> showList(ActivityUser entity, Pager pager, List<OrderHelper> helper) throws BizException {
		// TODO Auto-generated method stub
		return null;
	}

}
